;;;; numbers.test --- tests guile's numbers     -*- scheme -*-
;;;; Copyright (C) 2000, 2001, 2004 Free Software Foundation, Inc.
;;;;
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2, or (at your option)
;;;; any later version.
;;;;
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU General Public License
;;;; along with this software; see the file COPYING.  If not, write to
;;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
;;;; Boston, MA 02111-1307 USA
;;;;
;;;; As a special exception, the Free Software Foundation gives permission
;;;; for additional uses of the text contained in its release of GUILE.
;;;;
;;;; The exception is that, if you link the GUILE library with other files
;;;; to produce an executable, this does not by itself cause the
;;;; resulting executable to be covered by the GNU General Public License.
;;;; Your use of that executable is in no way restricted on account of
;;;; linking the GUILE library code into it.
;;;;
;;;; This exception does not however invalidate any other reasons why
;;;; the executable file might be covered by the GNU General Public License.
;;;;
;;;; This exception applies only to the code released by the
;;;; Free Software Foundation under the name GUILE.  If you copy
;;;; code from other Free Software Foundation releases into a copy of
;;;; GUILE, as the General Public License permits, the exception does
;;;; not apply to the code that you add in this way.  To avoid misleading
;;;; anyone as to the status of such modified files, you must delete
;;;; this exception notice from them.
;;;;
;;;; If you write modifications of your own for GUILE, it is your choice
;;;; whether to permit this exception to apply to your modifications.
;;;; If you do not wish that, delete this exception notice.

(use-modules (ice-9 documentation))

;;;
;;; miscellaneous
;;;

(define (documented? object)
  (not (not (object-documentation object))))

(define fixnum-bit
  (inexact->exact (+ (/ (log (+ most-positive-fixnum 1)) (log 2)) 1)))

(define fixnum-min most-negative-fixnum)
(define fixnum-max most-positive-fixnum)


;;;
;;; exact?
;;;

(with-test-prefix "exact?"

  (pass-if "documented?"
    (documented? exact?))

  (with-test-prefix "integers"

    (pass-if "0"
      (exact? 0))

    (pass-if "fixnum-max"
      (exact? fixnum-max))

    (pass-if "fixnum-max + 1"
      (exact? (+ fixnum-max 1)))

    (pass-if "fixnum-min"
      (exact? fixnum-min))

    (pass-if "fixnum-min - 1"
      (exact? (- fixnum-min 1))))

  (with-test-prefix "reals"

    ;; (FIXME: need better examples.)

    (pass-if "sqrt (fixnum-max^2 - 1)"
      (eq? #f (exact? (sqrt (- (expt fixnum-max 2) 1)))))

    (pass-if "sqrt ((fixnum-max+1)^2 - 1)"
      (eq? #f (exact? (sqrt (- (expt (+ fixnum-max 1) 2) 1)))))))

;;;
;;; odd?
;;;


;;;
;;; even?
;;;


;;;
;;; abs
;;;


;;;
;;; quotient
;;;

(with-test-prefix "quotient"

  (expect-fail "documented?"
    (documented? quotient))

  (with-test-prefix "0 / n"

    (pass-if "n = 1"
      (eqv? 0 (quotient 0 1)))

    (pass-if "n = -1"
      (eqv? 0 (quotient 0 -1)))

    (pass-if "n = 2"
      (eqv? 0 (quotient 0 2)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (quotient 0 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (quotient 0 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (quotient 0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient 0 (- fixnum-min 1)))))

  (with-test-prefix "1 / n"

    (pass-if "n = 1"
      (eqv? 1 (quotient 1 1)))

    (pass-if "n = -1"
      (eqv? -1 (quotient 1 -1)))

    (pass-if "n = 2"
      (eqv? 0 (quotient 1 2)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (quotient 1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (quotient 1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (quotient 1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient 1 (- fixnum-min 1)))))

  (with-test-prefix "-1 / n"

    (pass-if "n = 1"
      (eqv? -1 (quotient -1 1)))

    (pass-if "n = -1"
      (eqv? 1 (quotient -1 -1)))

    (pass-if "n = 2"
      (eqv? 0 (quotient -1 2)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (quotient -1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (quotient -1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (quotient -1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient -1 (- fixnum-min 1)))))

  (with-test-prefix "fixnum-max / n"

    (pass-if "n = 1"
      (eqv? fixnum-max (quotient fixnum-max 1)))

    (pass-if "n = -1"
      (eqv? (- fixnum-max) (quotient fixnum-max -1)))

    (pass-if "n = 2"
      (eqv? fixnum-max (+ (* (quotient fixnum-max 2) 2) 1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (quotient fixnum-max fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (quotient fixnum-max (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (quotient fixnum-max fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient fixnum-max (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-max + 1) / n"

    (pass-if "n = 1"
      (eqv? (+ fixnum-max 1) (quotient (+ fixnum-max 1) 1)))

    (pass-if "n = -1"
      (eqv? (- (+ fixnum-max 1)) (quotient (+ fixnum-max 1) -1)))

    (pass-if "n = 2"
      (eqv? (+ fixnum-max 1) (* (quotient (+ fixnum-max 1) 2) 2)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (quotient (+ fixnum-max 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (quotient (+ fixnum-max 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (quotient (+ fixnum-max 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient (+ fixnum-max 1) (- fixnum-min 1)))))

  (with-test-prefix "fixnum-min / n"

    (pass-if "n = 1"
      (eqv? fixnum-min (quotient fixnum-min 1)))

    (pass-if "n = -1"
      (eqv? (- fixnum-min) (quotient fixnum-min -1)))

    (pass-if "n = 2"
      (eqv? fixnum-min (* (quotient fixnum-min 2) 2)))

    (pass-if "n = fixnum-max"
      (eqv? -1 (quotient fixnum-min fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? -1 (quotient fixnum-min (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (quotient fixnum-min fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (quotient fixnum-min (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-min - 1) / n"

    (pass-if "n = 1"
      (eqv? (- fixnum-min 1) (quotient (- fixnum-min 1) 1)))

    (pass-if "n = -1"
      (eqv? (- (- fixnum-min 1)) (quotient (- fixnum-min 1) -1)))

    (pass-if "n = 2"
      (eqv? fixnum-min (* (quotient (- fixnum-min 1) 2) 2)))

    (pass-if "n = fixnum-max"
      (eqv? -1 (quotient (- fixnum-min 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? -1 (quotient (- fixnum-min 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (quotient (- fixnum-min 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (quotient (- fixnum-min 1) (- fixnum-min 1)))))
  
  ;; Positive dividend and divisor

  (pass-if "35 / 7"
    (eqv? 5 (quotient 35 7)))

  ;; Negative dividend, positive divisor

  (pass-if "-35 / 7"
    (eqv? -5 (quotient -35 7)))

  ;; Positive dividend, negative divisor

  (pass-if "35 / -7"
    (eqv? -5 (quotient 35 -7)))

  ;; Negative dividend and divisor

  (pass-if "-35 / -7"
    (eqv? 5 (quotient -35 -7)))

  ;; Are numerical overflows detected correctly?

  ;; Are wrong type arguments detected correctly?

  )

;;;
;;; remainder
;;;

(with-test-prefix "remainder"

  (expect-fail "documented?"
    (documented? remainder))

  (with-test-prefix "0 / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder 0 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder 0 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (remainder 0 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (remainder 0 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (remainder 0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (remainder 0 (- fixnum-min 1)))))

  (with-test-prefix "1 / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder 1 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder 1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (remainder 1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (remainder 1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (remainder 1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (remainder 1 (- fixnum-min 1)))))

  (with-test-prefix "-1 / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder -1 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder -1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? -1 (remainder -1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? -1 (remainder -1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (remainder -1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? -1 (remainder -1 (- fixnum-min 1)))))

  (with-test-prefix "fixnum-max / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder fixnum-max 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder fixnum-max -1)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (remainder fixnum-max fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? fixnum-max (remainder fixnum-max (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? fixnum-max (remainder fixnum-max fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? fixnum-max (remainder fixnum-max (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-max + 1) / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder (+ fixnum-max 1) 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder (+ fixnum-max 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (remainder (+ fixnum-max 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (remainder (+ fixnum-max 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (remainder (+ fixnum-max 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? (+ fixnum-max 1) (remainder (+ fixnum-max 1) (- fixnum-min 1)))))

  (with-test-prefix "fixnum-min / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder fixnum-min 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder fixnum-min -1)))

    (pass-if "n = fixnum-max"
      (eqv? -1 (remainder fixnum-min fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (remainder fixnum-min (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (remainder fixnum-min fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? fixnum-min (remainder fixnum-min (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-min - 1) / n"

    (pass-if "n = 1"
      (eqv? 0 (remainder (- fixnum-min 1) 1)))

    (pass-if "n = -1"
      (eqv? 0 (remainder (- fixnum-min 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? -2 (remainder (- fixnum-min 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? -1 (remainder (- fixnum-min 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (remainder (- fixnum-min 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (remainder (- fixnum-min 1) (- fixnum-min 1)))))

  ;; Positive dividend and divisor

  (pass-if "35 / 7"
    (eqv? 0 (remainder 35 7)))

  ;; Negative dividend, positive divisor

  (pass-if "-35 / 7"
    (eqv? 0 (remainder -35 7)))

  ;; Positive dividend, negative divisor

  (pass-if "35 / -7"
    (eqv? 0 (remainder 35 -7)))

  ;; Negative dividend and divisor

  (pass-if "-35 / -7"
    (eqv? 0 (remainder -35 -7)))

  ;; Are numerical overflows detected correctly?

  ;; Are wrong type arguments detected correctly?

  )

;;;
;;; modulo
;;;

(with-test-prefix "modulo"

  (expect-fail "documented?"
    (documented? modulo))

  (with-test-prefix "0 % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo 0 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo 0 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (modulo 0 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (modulo 0 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (modulo 0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (modulo 0 (- fixnum-min 1)))))

  (with-test-prefix "1 % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo 1 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo 1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (modulo 1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (modulo 1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? (+ fixnum-min 1) (modulo 1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? fixnum-min (modulo 1 (- fixnum-min 1)))))

  (with-test-prefix "-1 % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo -1 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo -1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? (- fixnum-max 1) (modulo -1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? fixnum-max (modulo -1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (modulo -1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? -1 (modulo -1 (- fixnum-min 1)))))

  (with-test-prefix "fixnum-max % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo fixnum-max 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo fixnum-max -1)))

    (pass-if "n = fixnum-max"
      (eqv? 0 (modulo fixnum-max fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? fixnum-max (modulo fixnum-max (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (modulo fixnum-max fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? -2 (modulo fixnum-max (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-max + 1) % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo (+ fixnum-max 1) 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo (+ fixnum-max 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (modulo (+ fixnum-max 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (modulo (+ fixnum-max 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (modulo (+ fixnum-max 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? -1 (modulo (+ fixnum-max 1) (- fixnum-min 1)))))

  (with-test-prefix "fixnum-min % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo fixnum-min 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo fixnum-min -1)))

    (pass-if "n = fixnum-max"
      (eqv? (- fixnum-max 1) (modulo fixnum-min fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 0 (modulo fixnum-min (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 0 (modulo fixnum-min fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? fixnum-min (modulo fixnum-min (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-min - 1) % n"

    (pass-if "n = 1"
      (eqv? 0 (modulo (- fixnum-min 1) 1)))

    (pass-if "n = -1"
      (eqv? 0 (modulo (- fixnum-min 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? (- fixnum-max 2) (modulo (- fixnum-min 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? fixnum-max (modulo (- fixnum-min 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? -1 (modulo (- fixnum-min 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 0 (modulo (- fixnum-min 1) (- fixnum-min 1)))))

  ;; Positive dividend and divisor

  (pass-if "13 % 4"
    (eqv? 1 (modulo 13 4)))

  (pass-if "2177452800 % 86400"
    (eqv? 0 (modulo 2177452800 86400)))

  ;; Negative dividend, positive divisor

  (pass-if "-13 % 4"
    (eqv? 3 (modulo -13 4)))

  (pass-if "-2177452800 % 86400"
    (eqv? 0 (modulo -2177452800 86400)))

  ;; Positive dividend, negative divisor

  (pass-if "13 % -4"
    (eqv? -3 (modulo 13 -4)))

  (pass-if "2177452800 % -86400"
    (eqv? 0 (modulo 2177452800 -86400)))

  ;; Negative dividend and divisor

  (pass-if "-13 % -4"
    (eqv? -1 (modulo -13 -4)))

  (pass-if "-2177452800 % -86400"
    (eqv? 0 (modulo -2177452800 -86400)))

  ;; Are numerical overflows detected correctly?

  ;; Are wrong type arguments detected correctly?

  )

;;;
;;; gcd
;;;

(with-test-prefix "gcd"

  (expect-fail "documented?"
    (documented? gcd))

  (with-test-prefix "(0 n)"

    (pass-if "n = 0"
      (eqv? 0 (gcd 0 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd 0 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd 0 -1)))

    (pass-if "n = fixnum-max"
      (eqv? fixnum-max (gcd 0 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? (+ fixnum-max 1) (gcd 0 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? (- fixnum-min) (gcd 0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? (- (- fixnum-min 1)) (gcd 0 (- fixnum-min 1)))))

  (with-test-prefix "(1 n)"

    (pass-if "n = 0"
      (eqv? 1 (gcd 1 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd 1 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd 1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (gcd 1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (gcd 1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (gcd 1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (gcd 1 (- fixnum-min 1)))))

  (with-test-prefix "(-1 n)"

    (pass-if "n = 0"
      (eqv? 1 (gcd -1 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd -1 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd -1 -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (gcd -1 fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (gcd -1 (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (gcd -1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (gcd -1 (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-max n)"

    (pass-if "n = 0"
      (eqv? fixnum-max (gcd fixnum-max 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd fixnum-max 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd fixnum-max -1)))

    (pass-if "n = fixnum-max"
      (eqv? fixnum-max (gcd fixnum-max fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (gcd fixnum-max (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (gcd fixnum-max fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (gcd fixnum-max (- fixnum-min 1)))))

  (with-test-prefix "((+ fixnum-max 1) n)"

    (pass-if "n = 0"
      (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd (+ fixnum-max 1) 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd (+ fixnum-max 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (gcd (+ fixnum-max 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? (+ fixnum-max 1) (gcd (+ fixnum-max 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (gcd (+ fixnum-max 1) (- fixnum-min 1)))))

  (with-test-prefix "(fixnum-min n)"

    (pass-if "n = 0"
      (eqv? (- fixnum-min) (gcd fixnum-min 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd fixnum-min 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd fixnum-min -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (gcd fixnum-min fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? (+ fixnum-max 1) (gcd fixnum-min (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? (- fixnum-min) (gcd fixnum-min fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? 1 (gcd fixnum-min (- fixnum-min 1)))))

  (with-test-prefix "((- fixnum-min 1) n)"

    (pass-if "n = 0"
      (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) 0)))

    (pass-if "n = 1"
      (eqv? 1 (gcd (- fixnum-min 1) 1)))

    (pass-if "n = -1"
      (eqv? 1 (gcd (- fixnum-min 1) -1)))

    (pass-if "n = fixnum-max"
      (eqv? 1 (gcd (- fixnum-min 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (eqv? 1 (gcd (- fixnum-min 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (eqv? 1 (gcd (- fixnum-min 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (eqv? (- (- fixnum-min 1)) (gcd (- fixnum-min 1) (- fixnum-min 1)))))

  ;; Are wrong type arguments detected correctly?

  )

;;;
;;; lcm
;;;

;;;
;;; number->string
;;;

;;;
;;; string->number
;;;


(with-test-prefix "string->number"

  (pass-if "documented?"
    (documented? string->number))

  ;; Prior to Guile 1.6.7 the bignum size calculation (used for strings >=
  ;; 6 chars) was wrong (for bases other than 2, 10 and 16), resulting in
  ;; numerical overflow errors for certain conversions.
  ;;
  ;; The following exercises string->number of strings like "999999" (each
  ;; digit is base-1) in bases 2 to 16.
  ;;
  (do ((base 2 (1+ base)))
      ((> base 16))

    (with-test-prefix (list 'base base)

      (do ((digit (string-ref (number->string (1- base) base) 0))
	   (want  (1- base)   (+ (* want base) base -1))
	   (len   1           (1+ len)))
	  ((> len 300))

	(pass-if (list 'length len)
	  (eqv? want (string->number (make-string len digit) base)))))))

;;;
;;; number?
;;;

;;;
;;; complex?
;;;

;;;
;;; real?
;;;

;;;
;;; rational?
;;;

;;;
;;; integer?
;;;

;;;
;;; inexact?
;;;

;;;
;;; =
;;;

;;;
;;; <
;;;

(with-test-prefix "<"

  (expect-fail "documented?"
    (documented? <))

  (with-test-prefix "(< 0 n)"
    
    (pass-if "n = 0"
      (not (< 0 0)))
    
    (pass-if "n = 0.0"
      (not (< 0 0.0)))
    
    (pass-if "n = 1"
      (< 0 1))
    
    (pass-if "n = 1.0"
      (< 0 1.0))
    
    (pass-if "n = -1"
      (not (< 0 -1)))
    
    (pass-if "n = -1.0"
      (not (< 0 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< 0 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< 0 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< 0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< 0 (- fixnum-min 1)))))
  
  (with-test-prefix "(< 0.0 n)"
    
    (pass-if "n = 0"
      (not (< 0.0 0)))
    
    (pass-if "n = 0.0"
      (not (< 0.0 0.0)))
    
    (pass-if "n = 1"
      (< 0.0 1))
    
    (pass-if "n = 1.0"
      (< 0.0 1.0))
    
    (pass-if "n = -1"
      (not (< 0.0 -1)))
    
    (pass-if "n = -1.0"
      (not (< 0.0 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< 0.0 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< 0.0 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< 0.0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< 0.0 (- fixnum-min 1)))))
  
  (with-test-prefix "(< 1 n)"
    
    (pass-if "n = 0"
      (not (< 1 0)))
    
    (pass-if "n = 0.0"
      (not (< 1 0.0)))
    
    (pass-if "n = 1"
      (not (< 1 1)))
    
    (pass-if "n = 1.0"
      (not (< 1 1.0)))
    
    (pass-if "n = -1"
      (not (< 1 -1)))
    
    (pass-if "n = -1.0"
      (not (< 1 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< 1 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< 1 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< 1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< 1 (- fixnum-min 1)))))

  (with-test-prefix "(< 1.0 n)"
    
    (pass-if "n = 0"
      (not (< 1.0 0)))
    
    (pass-if "n = 0.0"
      (not (< 1.0 0.0)))
    
    (pass-if "n = 1"
      (not (< 1.0 1)))
    
    (pass-if "n = 1.0"
      (not (< 1.0 1.0)))
    
    (pass-if "n = -1"
      (not (< 1.0 -1)))
    
    (pass-if "n = -1.0"
      (not (< 1.0 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< 1.0 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< 1.0 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< 1.0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< 1.0 (- fixnum-min 1)))))
  
  (with-test-prefix "(< -1 n)"
    
    (pass-if "n = 0"
      (< -1 0))
    
    (pass-if "n = 0.0"
      (< -1 0.0))
    
    (pass-if "n = 1"
      (< -1 1))
    
    (pass-if "n = 1.0"
      (< -1 1.0))
    
    (pass-if "n = -1"
      (not (< -1 -1)))
    
    (pass-if "n = -1.0"
      (not (< -1 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< -1 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< -1 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< -1 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< -1 (- fixnum-min 1)))))

  (with-test-prefix "(< -1.0 n)"
    
    (pass-if "n = 0"
      (< -1.0 0))
    
    (pass-if "n = 0.0"
      (< -1.0 0.0))
    
    (pass-if "n = 1"
      (< -1.0 1))
    
    (pass-if "n = 1.0"
      (< -1.0 1.0))
    
    (pass-if "n = -1"
      (not (< -1.0 -1)))
    
    (pass-if "n = -1.0"
      (not (< -1.0 -1.0)))
    
    (pass-if "n = fixnum-max"
      (< -1.0 fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< -1.0 (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< -1.0 fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< -1.0 (- fixnum-min 1)))))

  (with-test-prefix "(< fixnum-max n)"
    
    (pass-if "n = 0"
      (not (< fixnum-max 0)))
    
    (pass-if "n = 0.0"
      (not (< fixnum-max 0.0)))
    
    (pass-if "n = 1"
      (not (< fixnum-max 1)))
    
    (pass-if "n = 1.0"
      (not (< fixnum-max 1.0)))
    
    (pass-if "n = -1"
      (not (< fixnum-max -1)))
    
    (pass-if "n = -1.0"
      (not (< fixnum-max -1.0)))
    
    (pass-if "n = fixnum-max"
      (not (< fixnum-max fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (< fixnum-max (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< fixnum-max fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< fixnum-max (- fixnum-min 1)))))

  (with-test-prefix "(< (+ fixnum-max 1) n)"
    
    (pass-if "n = 0"
      (not (< (+ fixnum-max 1) 0)))
    
    (pass-if "n = 0.0"
      (not (< (+ fixnum-max 1) 0.0)))
    
    (pass-if "n = 1"
      (not (< (+ fixnum-max 1) 1)))
    
    (pass-if "n = 1.0"
      (not (< (+ fixnum-max 1) 1.0)))
    
    (pass-if "n = -1"
      (not (< (+ fixnum-max 1) -1)))
    
    (pass-if "n = -1.0"
      (not (< (+ fixnum-max 1) -1.0)))
    
    (pass-if "n = fixnum-max"
      (not (< (+ fixnum-max 1) fixnum-max)))

    (pass-if "n = fixnum-max + 1"
      (not (< (+ fixnum-max 1) (+ fixnum-max 1))))

    (pass-if "n = fixnum-min"
      (not (< (+ fixnum-max 1) fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< (+ fixnum-max 1) (- fixnum-min 1)))))

  (with-test-prefix "(< fixnum-min n)"
    
    (pass-if "n = 0"
      (< fixnum-min 0))
    
    (pass-if "n = 0.0"
      (< fixnum-min 0.0))
    
    (pass-if "n = 1"
      (< fixnum-min 1))
    
    (pass-if "n = 1.0"
      (< fixnum-min 1.0))
    
    (pass-if "n = -1"
      (< fixnum-min -1))
    
    (pass-if "n = -1.0"
      (< fixnum-min -1.0))
    
    (pass-if "n = fixnum-max"
      (< fixnum-min fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< fixnum-min (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (not (< fixnum-min fixnum-min)))

    (pass-if "n = fixnum-min - 1"
      (not (< fixnum-min (- fixnum-min 1)))))

  (with-test-prefix "(< (- fixnum-min 1) n)"
    
    (pass-if "n = 0"
      (< (- fixnum-min 1) 0))
    
    (pass-if "n = 0.0"
      (< (- fixnum-min 1) 0.0))
    
    (pass-if "n = 1"
      (< (- fixnum-min 1) 1))
    
    (pass-if "n = 1.0"
      (< (- fixnum-min 1) 1.0))
    
    (pass-if "n = -1"
      (< (- fixnum-min 1) -1))
    
    (pass-if "n = -1.0"
      (< (- fixnum-min 1) -1.0))
    
    (pass-if "n = fixnum-max"
      (< (- fixnum-min 1) fixnum-max))

    (pass-if "n = fixnum-max + 1"
      (< (- fixnum-min 1) (+ fixnum-max 1)))

    (pass-if "n = fixnum-min"
      (< (- fixnum-min 1) fixnum-min))

    (pass-if "n = fixnum-min - 1"
      (not (< (- fixnum-min 1) (- fixnum-min 1))))))

;;;
;;; >
;;;

;;;
;;; <=
;;;

;;;
;;; >=
;;;

;;;
;;; zero?
;;;

;;;
;;; positive?
;;;

;;;
;;; negative?
;;;

;;;
;;; max
;;;

;;;
;;; min
;;;

;;;
;;; +
;;;

(with-test-prefix "+"

  (expect-fail "documented?"
    (documented? +))

  (with-test-prefix "wrong type argument"

    (pass-if-exception "1st argument string"
      exception:wrong-type-arg
      (+ "1" 2))

    (pass-if-exception "2nd argument bool"
      exception:wrong-type-arg
      (+ 1 #f))))
;;;
;;; -
;;;

;;;
;;; *
;;;

;;;
;;; /
;;;

;;;
;;; truncate
;;;

(with-test-prefix "truncate"
  (with-test-prefix "inum"
    (pass-if "0"
      (and (= 0    (truncate 0))
	   (exact? (truncate 0))))

    (pass-if "1"
      (and (= 1    (truncate 1))
	   (exact? (truncate 1))))

    (pass-if "-1"
      (and (= -1   (truncate -1))
	   (exact? (truncate -1)))))

  (with-test-prefix "bignum"
    (let ((x (1+ most-positive-fixnum)))
      (pass-if "(1+ most-positive-fixnum)"
	(and (= x    (truncate x))
	     (exact? (truncate x)))))

    (let ((x (1- most-negative-fixnum)))
      (pass-if "(1- most-negative-fixnum)"
	(and (= x    (truncate x))
	     (exact? (truncate x))))))

  (with-test-prefix "real"
    (pass-if "0.0"
      (and (= 0.0    (truncate 0.0))
	   (inexact? (truncate 0.0))))

    (pass-if "1.0"
      (and (= 1.0    (truncate 1.0))
	   (inexact? (truncate 1.0))))

    (pass-if "-1.0"
      (and (= -1.0   (truncate -1.0))
	   (inexact? (truncate -1.0))))

    (pass-if "3.9"
      (and (= 3.0    (truncate 3.9))
	   (inexact? (truncate 3.9))))

    (pass-if "-3.9"
      (and (= -3.0   (truncate -3.9))
	   (inexact? (truncate -3.9))))))

;;;
;;; round
;;;

(with-test-prefix "round"
  (with-test-prefix "inum"
    (pass-if "0"
      (and (= 0    (round 0))
	   (exact? (round 0))))

    (pass-if "1"
      (and (= 1    (round 1))
	   (exact? (round 1))))

    (pass-if "-1"
      (and (= -1   (round -1))
	   (exact? (round -1)))))

  (with-test-prefix "bignum"
    (let ((x (1+ most-positive-fixnum)))
      (pass-if "(1+ most-positive-fixnum)"
	(and (= x    (round x))
	     (exact? (round x)))))

    (let ((x (1- most-negative-fixnum)))
      (pass-if "(1- most-negative-fixnum)"
	(and (= x    (round x))
	     (exact? (round x))))))

  (with-test-prefix "real"
    (pass-if "0.0"
      (and (= 0.0    (round 0.0))
	   (inexact? (round 0.0))))

    (pass-if "1.0"
      (and (= 1.0    (round 1.0))
	   (inexact? (round 1.0))))

    (pass-if "-1.0"
      (and (= -1.0   (round -1.0))
	   (inexact? (round -1.0))))

    (pass-if "-3.1"
      (and (= -3.0   (round -3.1))
	   (inexact? (round -3.1))))

    (pass-if "3.1"
      (and (= 3.0    (round 3.1))
	   (inexact? (round 3.1))))

    (pass-if "3.9"
      (and (= 4.0    (round 3.9))
	   (inexact? (round 3.9))))

    (pass-if "-3.9"
      (and (= -4.0   (round -3.9))
	   (inexact? (round -3.9))))

    (pass-if "1.5"
      (and (= 2.0    (round 1.5))
	   (inexact? (round 1.5))))

    (pass-if "2.5"
      (and (= 2.0    (round 2.5))
	   (inexact? (round 2.5))))

    (pass-if "3.5"
      (and (= 4.0    (round 3.5))
	   (inexact? (round 3.5))))

    (pass-if "-1.5"
      (and (= -2.0   (round -1.5))
	   (inexact? (round -1.5))))

    (pass-if "-2.5"
      (and (= -2.0   (round -2.5))
	   (inexact? (round -2.5))))

    (pass-if "-3.5"
      (and (= -4.0   (round -3.5))
	   (inexact? (round -3.5))))

    ;; prior to guile 1.6.5, on an IEEE system an inexact 2^53-1 (ie. a
    ;; float with mantissa all ones) came out as 2^53 from `round' (except
    ;; on i386 and m68k systems using the coprocessor and optimizing, where
    ;; extra precision hid the problem)
    (pass-if "2^53-1"
      (let ((x (exact->inexact (1- (ash 1 53)))))
	(and (= x      (round x))
	     (inexact? (round x)))))
    (pass-if "-(2^53-1)"
      (let ((x (exact->inexact (- (1- (ash 1 53))))))
	(and (= x      (round x))
	     (inexact? (round x)))))))

;;;
;;; exact->inexact
;;;

;;;
;;; floor
;;;

(with-test-prefix "floor"
  (with-test-prefix "inum"
    (pass-if "0"
      (and (= 0    (floor 0))
	   (exact? (floor 0))))

    (pass-if "1"
      (and (= 1    (floor 1))
	   (exact? (floor 1))))

    (pass-if "-1"
      (and (= -1   (floor -1))
	   (exact? (floor -1)))))

  (with-test-prefix "bignum"
    (let ((x (1+ most-positive-fixnum)))
      (pass-if "(1+ most-positive-fixnum)"
	(and (= x    (floor x))
	     (exact? (floor x)))))

    (let ((x (1- most-negative-fixnum)))
      (pass-if "(1- most-negative-fixnum)"
	(and (= x    (floor x))
	     (exact? (floor x))))))

  (with-test-prefix "real"
    (pass-if "0.0"
      (and (= 0.0    (floor 0.0))
	   (inexact? (floor 0.0))))

    (pass-if "1.0"
      (and (= 1.0    (floor 1.0))
	   (inexact? (floor 1.0))))

    (pass-if "-1.0"
      (and (= -1.0   (floor -1.0))
	   (inexact? (floor -1.0))))

    (pass-if "3.9"
      (and (= 3.0    (floor 3.9))
	   (inexact? (floor 3.9))))

    (pass-if "-3.9"
      (and (= -4.0   (floor -3.9))
	   (inexact? (floor -3.9))))))

;;;
;;; ceiling
;;;

(with-test-prefix "ceiling"
  (with-test-prefix "inum"
    (pass-if "0"
      (and (= 0    (ceiling 0))
	   (exact? (ceiling 0))))

    (pass-if "1"
      (and (= 1    (ceiling 1))
	   (exact? (ceiling 1))))

    (pass-if "-1"
      (and (= -1   (ceiling -1))
	   (exact? (ceiling -1)))))

  (with-test-prefix "bignum"
    (let ((x (1+ most-positive-fixnum)))
      (pass-if "(1+ most-positive-fixnum)"
	(and (= x    (ceiling x))
	     (exact? (ceiling x)))))

    (let ((x (1- most-negative-fixnum)))
      (pass-if "(1- most-negative-fixnum)"
	(and (= x    (ceiling x))
	     (exact? (ceiling x))))))

  (with-test-prefix "real"
    (pass-if "0.0"
      (and (= 0.0    (ceiling 0.0))
	   (inexact? (ceiling 0.0))))

    (pass-if "1.0"
      (and (= 1.0    (ceiling 1.0))
	   (inexact? (ceiling 1.0))))

    (pass-if "-1.0"
      (and (= -1.0   (ceiling -1.0))
	   (inexact? (ceiling -1.0))))

    (pass-if "3.9"
      (and (= 4.0    (ceiling 3.9))
	   (inexact? (ceiling 3.9))))

    (pass-if "-3.9"
      (and (= -3.0   (ceiling -3.9))
	   (inexact? (ceiling -3.9))))))

;;;
;;; sqrt
;;;

;;;
;;; expt
;;;

(with-test-prefix "expt"
  (pass-if "(= 1 (expt 0 0))" (= 1 (expt 0 0)))
  (pass-if "(= 1 (expt 0 0.0))" (= 1 (expt 0 0.0)))
  (pass-if "(= 1 (expt 0.0 0))" (= 1 (expt 0.0 0)))
  (pass-if "(= 1 (expt 0.0 0.0))" (= 1 (expt 0.0 0.0))))

;;;
;;; make-rectangular
;;;

;;;
;;; make-polar
;;;

;;;
;;; real-part
;;;

;;;
;;; imag-part
;;;

;;;
;;; magnitude
;;;

;;;
;;; angle
;;;

;;;
;;; inexact->exact
;;;

;;;
;;; logbit?
;;;

(with-test-prefix "logbit?"
  (pass-if "0 0"  (eq? #f (logbit?  0 0)))
  (pass-if "1 0"  (eq? #f (logbit?  1 0)))
  (pass-if "31 0" (eq? #f (logbit? 31 0)))
  (pass-if "32 0" (eq? #f (logbit? 32 0)))
  (pass-if "33 0" (eq? #f (logbit? 33 0)))
  (pass-if "63 0" (eq? #f (logbit? 63 0)))
  (pass-if "64 0" (eq? #f (logbit? 64 0)))
  (pass-if "65 0" (eq? #f (logbit? 65 0)))

  ;; prior to guile 1.6.5, testing bit 32, 64 etc of value 1 would wrap
  ;; around and return #t where it ought to be #f
  (pass-if "0 1"  (eq? #t (logbit?  0 1)))
  (pass-if "1 1"  (eq? #f (logbit?  1 1)))
  (pass-if "31 1" (eq? #f (logbit? 31 1)))
  (pass-if "32 1" (eq? #f (logbit? 32 1)))
  (pass-if "33 1" (eq? #f (logbit? 33 1)))
  (pass-if "63 1" (eq? #f (logbit? 63 1)))
  (pass-if "64 1" (eq? #f (logbit? 64 1)))
  (pass-if "65 1" (eq? #f (logbit? 65 1)))
  (pass-if "128 1" (eq? #f (logbit? 128 1)))

  (pass-if "0 -1"  (eq? #t (logbit?  0 -1)))
  (pass-if "1 -1"  (eq? #t (logbit?  1 -1)))
  (pass-if "31 -1" (eq? #t (logbit? 31 -1)))
  (pass-if "32 -1" (eq? #t (logbit? 32 -1)))
  (pass-if "33 -1" (eq? #t (logbit? 33 -1)))
  (pass-if "63 -1" (eq? #t (logbit? 63 -1)))
  (pass-if "64 -1" (eq? #t (logbit? 64 -1)))
  (pass-if "65 -1" (eq? #t (logbit? 65 -1))))
